﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AkasaProject.Model;
using AkasaProject.DB;
using Util;
using System.Data;

namespace AkasaProject.Service
{
    public class MainService
    {
        /// <summary>
        /// 更新所有字段
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public int update(MainModel mm,string where="") {
            string sql = "update "+MainDB.TableName+" set "+MainDB.NoteText+"='" + mm.noteText +
                "',"+MainDB.JinduText+"='" + mm.jinduText + "',"+MainDB.XuqiuText+"='" + mm.xuqiuText + 
                "',"+MainDB.CeshiText+"='" + mm.ceshiText+ "',"+MainDB.YanshouText+"='" +mm.yanshouText + 
                "',"+MainDB.FujianText+"='" + mm.fujianText + "',"+MainDB.ChangeDatte+"='" + mm.changedate+"'";
            if (!string.IsNullOrEmpty(where))
                sql += " where " + where;
            else
                sql += " where "+MainDB.ID+"='"+mm.ID+"'";

            return SQLHelper.ExecuteSql(sql);
        }
        /// <summary>
        /// 更新自定义字段
        /// </summary>
        /// <param name="sets"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public int updateBySets(string sets, string where = "") {
            string sql = "update "+MainDB.TableName+" set "+sets;
            if (!string.IsNullOrEmpty(where))
                sql += " where " + where;
            return SQLHelper.ExecuteSql(sql);
        }
        /// <summary>
        /// 根据条件查询，返回dataset数据集
        /// </summary>
        /// <param name="fileds"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataSet select(string where = "", string fileds = "*") {
            string sql = "select "+fileds+" from "+MainDB.TableName+ " where "+where;
            return SQLHelper.Query(sql);
        }
        /// <summary>
        /// 插入新的数据
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public int insert(MainModel mm) {
            string sql = "insert into "+MainDB.TableName+" ("+
                MainDB.M_name+","+
                MainDB.JinduText+","+
                MainDB.XuqiuText+","+
                MainDB.CeshiText+","+
                MainDB.YanshouText+","+
                MainDB.NoteText+","+
                MainDB.ProfileId+","+
                MainDB.CreateDate+","+
                MainDB.Type+","+
                MainDB.FujianText+") values ('"+
                mm.m_name+"','"+
                mm.jinduText+"','"+
                mm.xuqiuText+"','"+
                mm.ceshiText+"','"+
                mm.yanshouText+"','"+
                mm.noteText+"','"+
                mm.profileId+"','"+
                DateTime.Now.ToString()+"','"+
                mm.type+"','"+
                mm.fujianText+"')";

            return SQLHelper.ExecuteSql(sql);
        }
    }
}
